package com.collection;
import com.exception.DemoException;
import java.util.Date;
import java.util.Objects;

/**
 * 自己实现一个ArrayList，帮助我们更好的理解ArrayList类的底层结构
 *
 * @author jiangbang
 * @date 2016/5/27
 */
public class MyArrayList {
    //java语言的关键字，变量修饰符，如果用transient声明一个实例变量，
    // 当对象存储时，它的值不需要维持。换句话来说就是，用transient关键字标记的成员变量不参与序列化过程。
    private transient Object[] elementData;

    private int size;

    public MyArrayList(){
        this(10);
    }

    public MyArrayList(int initialCapacity){
        if(initialCapacity < 0){
            throw new DemoException();
        }
        elementData = new Object[initialCapacity];
    }

    public int size(){
        return size;
    }

    public boolean isEmpty(){
        return size==0;
    }

    public void add(Object obj){
        //数组扩容加拷贝
        if(size>=elementData.length){
            Object[] newArray = new Object[size*2+1];
            System.arraycopy(elementData,0,newArray,0,elementData.length);
//            for(int i=0;i<elementData.length;i++){
//                newArray[i]=elementData[i];
//            }
            elementData = newArray ;
        }
      //  elementData[size++] = obj;  等同于下二行代码
        elementData[size] = obj;  //加之前size = 0
        size++;   //加之后size = 1
    }


    public void add(int index, Object object){
        if(size>=elementData.length){
            Object[] newArray = new Object[size*2+1];
            System.arraycopy(elementData,0,newArray,0,elementData.length);
//            for(int i=0;i<elementData.length;i++){
//                newArray[i]=elementData[i];
//            }
            elementData = newArray ;
        }


        System.arraycopy(elementData, index, elementData, index + 1,
                size - index);
        elementData[index] = object;
        size++;
    }

    public Object get(int index){
        if(index < 0 || index > size){
            throw new DemoException();
        }
        return elementData[index];
    }

    public void remove(int index){
        //删除指定对象
        if(index>=size){
            throw new DemoException("数组越界");
        }
        int numMoved = size - index - 1;
        if(numMoved > 0){  //如果删除的不是最后一个对象
            //System.arraycopy(elementData,index+1,elementData,index,numMoved);
            for(int i=0;i<numMoved;i++){
                elementData[index+i] = elementData[index+i+1];
            }
        }
       // elementData[--size] = null;
        elementData[size] = null;
        size--;
    }

    public boolean remove(Object object){
        if (object == null) {
            for (int index = 0; index < size; index++)
                if (elementData[index] == null) {
                    fastRemove(index);
                    return true;
                }
        } else {
            for (int index = 0; index < size; index++)
                if (object.equals(elementData[index])) {
                    fastRemove(index);
                    return true;
                }
        }
        return false;
    }


    public Object set(int index,Object object){
        Object oldValue = elementData[index];
        elementData[index] = object;
        return oldValue;
    }

    private void fastRemove(int index){
        int numMoved = size - index - 1;
        if (numMoved > 0){
            System.arraycopy(elementData, index+1, elementData, index,
                    numMoved);
        }
        elementData[--size] = null; // clear to let GC do its work
    }




    public static void main(String[] args) {
        MyArrayList list = new MyArrayList();
        list.add(111);
        list.add(new Date());
        list.add(333);
        list.add(new Date());
        list.add(new Date());
        System.out.println(list.size());
        System.out.println(list.isEmpty());
        System.out.println(list.get(2));
    }
}
